package com.bestv.search.management.command;

import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.model.SysUser;
import com.bestv.search.common.model.Type;
import com.bestv.search.common.service.TypeManager;
import com.bestv.search.common.util.CommandException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TypeCommand {

    protected final Log log = LogFactory.getLog(getClass());
    private TypeManager typeManager;

    public String find(SysUser user, Map<String, String[]> map, DataSourceKey dsKey) throws CommandException {
        String result = "";
        List<Type> types = typeManager.getTypes(dsKey);
        try {
            JSONArray array = typesToJson(types);
            result = "{'types' : " + array.toString() + "}";
        } catch (JSONException e) {
            log.error(e);
            throw new CommandException();
        }
        return result;
    }

    public String save(SysUser user, Map<String, String[]> map, DataSourceKey dsKey) {
        String result = "";
        String[] id = map.get("id");
        String[] name = map.get("typeName");
        String[] description = map.get("typeDescription");

        Type type = null;
        if ("".equals(id[0])) {
            Type t = typeManager.getTypeByName(dsKey, name[0]);
            if (t != null)
                return "{'result':'failure','message':'name'}";
            type = new Type();
            type.setName(name[0]);
            type.setDescription(description[0]);
            type.setId(0L);
            typeManager.saveType(dsKey, type);
            result = "{'result':'success','message':'new'}";
        } else {
            type = typeManager.getTypeById(dsKey, new Long(id[0]));
            if (type == null)
                type = new Type();
            type.setName(name[0]);
            type.setDescription(description[0]);
            typeManager.saveType(dsKey, type);
            result = "{'result':'success','message':'edit'}";
        }
        return result;
    }

    public String get(SysUser user, Map<String, String[]> map, DataSourceKey dsKey) throws CommandException {
        String result = "";
        String[] id = map.get("id");

        Type t = typeManager.getTypeById(dsKey, new Long(id[0]));
        try {
            JSONObject jsonObj = typeToJson(t);
            result = jsonObj.toString();
        } catch (JSONException e) {
            log.error(e);
            throw new CommandException();
        }
        return result;
    }

    private JSONObject typeToJson(Type t) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", t.getId());
        jsonObject.put("name", t.getName());
        jsonObject.put("description", t.getDescription());
        return jsonObject;
    }

    private JSONArray typesToJson(List<Type> types) throws JSONException {
        List<JSONObject> list = new ArrayList<JSONObject>();
        for (Type t : types) {
            list.add(typeToJson(t));
        }
        JSONArray jsonArray = new JSONArray(list.toArray());
        return jsonArray;
    }

    public TypeManager getTypeManager() {
        return typeManager;
    }

    public void setTypeManager(TypeManager typeManager) {
        this.typeManager = typeManager;
    }
}
